﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using C.Entities;

namespace C.Unite
{
    /// <summary>
    /// 用户权限处理类
    /// </summary>
    public class UserPower
    {

        #region 取得模块是否有权限
        /// <summary>
        /// 取得模块是否有权限:先取个人权限如无则再取权限组权限
        /// </summary>
        /// <param name="mid">模块ID</param>
        /// <returns></returns>
        public static bool HaveKey(int mid, int userid, int groupid, string qx)
        {
            bool n = false;
            List<Sys_PerInfo> PerList = DBCaChe.GetPerList().FindAll(delegate(Sys_PerInfo tpinfo) { return tpinfo.Userid == userid || tpinfo.Userid == groupid; }); ;
            int puser = PerList.FindAll(delegate(Sys_PerInfo tpi) { return tpi.Mid == mid && tpi.Userid == userid && tpi.Pertype == 0 && tpi.Pername.Contains(qx); }).Count;
            //List<Sys_PerInfo> perlistuser = DbSession.Default.From<Sys_PerInfo>().Where(Sys_PerInfo._.Mid == tm.Mid && Sys_PerInfo._.Userid == uid && Sys_PerInfo._.Pmtype == 1 && Sys_PerInfo._.Pertype == 0).ToList();
            if (puser > 0)
            {
                n = true;
            }
            int pgroup = PerList.FindAll(delegate(Sys_PerInfo tpi) { return tpi.Mid == mid && tpi.Userid == groupid && tpi.Pertype == 1 && tpi.Pername.Contains(qx); }).Count;
            //List<Sys_PerInfo> perlistgroup = DbSession.Default.From<Sys_PerInfo>().Where(Sys_PerInfo._.Mid == tm.Mid && Sys_PerInfo._.Userid == gid && Sys_PerInfo._.Pmtype == 1).ToList();
            if (pgroup > 0)
            {
                n = true;
            }
            return n;
        }
        #endregion

        /// <summary>
        /// 取得权限
        /// </summary>
        /// <returns></returns>
        public static string GetHaveKey()
        {
            string keyword = GetKeyWord().Trim();
            //if (!string.IsNullOrEmpty(HttpContext.Current.Request["t"]))
            //{
            //    keyword += "_t" + HttpContext.Current.Request["t"];
            //}
            Sys_Modules tm = GetModul(keyword);
            List<Sys_PerInfo> PerList = DBCaChe.GetPerList().FindAll(delegate(Sys_PerInfo tpinfo) { return tpinfo.Mid == tm.Mid; });
            string n = "";
            if (null != tm)
            {
                //List<Sys_PerInfo> PerList = GetPerList().FindAll(delegate(Sys_PerInfo tpinfo) { return (tpinfo.Userid == EAdminid() || tpinfo.Userid == EAdminGroupId()) && tpinfo.Mid == tm.Mid; });
                // TbsDbSession.Default.From<Sys_Modules>().Where(Sys_Modules._.Keyword == keyword).ToFirst();

                List<Sys_PerInfo> perlistuser = PerList.FindAll(delegate(Sys_PerInfo tpi) { return tpi.Userid == CookInfo.EAdminid() && tpi.Pertype == 0; }); //TbsDbSession.Default.From<Sys_PerInfo>().Where(Sys_PerInfo._.Mid == tm.Mid && Sys_PerInfo._.Userid == EAdminid() && Sys_PerInfo._.Pertype == 0).ToList();
                if (perlistuser.Count > 0)
                {
                    foreach (Sys_PerInfo item in perlistuser)
                    {
                        n += item.Pername + ",";
                    }
                }
                List<Sys_PerInfo> perlistgroup = PerList.FindAll(delegate(Sys_PerInfo tpi) { return tpi.Userid == CookInfo.EAdminGroupId() && tpi.Pertype == 1; });
                //List<Sys_PerInfo> perlistgroup = TbsDbSession.Default.From<Sys_PerInfo>().Where(Sys_PerInfo._.Mid == tm.Mid && Sys_PerInfo._.Userid == EAdminGroupId() && Sys_PerInfo._.Pertype == 1).ToList();
                if (perlistgroup.Count > 0)
                {
                    foreach (Sys_PerInfo item in perlistgroup)
                    {
                        n += item.Pername + ",";
                    }
                }
            }
            return n;
        }

        /// <summary>
        /// 根据模块取得权限
        /// </summary>
        /// <returns></returns>
        public static string GetKeyHaveKey(string keyword, int uid, int gid)
        {
            List<Sys_PerInfo> PerList = DBCaChe.GetPerList().FindAll(delegate(Sys_PerInfo tpinfo) { return tpinfo.Userid == uid || tpinfo.Userid == gid; });
            string n = "";
            Sys_Modules tm = DBCaChe.GetModelList().Find(delegate(Sys_Modules tms) { return tms.Keyword == keyword; });// TbsDbSession.Default.From<Sys_Modules>().Where(Sys_Modules._.Keyword == keyword).ToFirst();
            if (null != tm)
            {
                List<Sys_PerInfo> perlistuser = PerList.FindAll(delegate(Sys_PerInfo tpi) { return tpi.Mid == tm.Mid && tpi.Userid == uid && tpi.Pmtype == 1 && tpi.Pertype == 0; });
                //List<Sys_PerInfo> perlistuser = TbsDbSession.Default.From<Sys_PerInfo>().Where(Sys_PerInfo._.Mid == tm.Mid && Sys_PerInfo._.Userid == uid && Sys_PerInfo._.Pmtype == 1 && Sys_PerInfo._.Pertype == 0).ToList();
                if (perlistuser.Count > 0)
                {
                    foreach (Sys_PerInfo item in perlistuser)
                    {
                        n += item.Pername + ",";
                    }
                }
                List<Sys_PerInfo> perlistgroup = PerList.FindAll(delegate(Sys_PerInfo tpi) { return tpi.Mid == tm.Mid && tpi.Userid == gid && tpi.Pmtype == 1 && tpi.Pertype == 1; });
                //List<Sys_PerInfo> perlistgroup = TbsDbSession.Default.From<Sys_PerInfo>().Where(Sys_PerInfo._.Mid == tm.Mid && Sys_PerInfo._.Userid == gid && Sys_PerInfo._.Pmtype == 1).ToList();
                if (perlistgroup.Count > 0)
                {
                    foreach (Sys_PerInfo item in perlistgroup)
                    {
                        n += item.Pername + ",";
                    }
                }
            }
            return n;
        }

        /// <summary>
        /// 取得当前页的模块名
        /// </summary>
        /// <returns></returns>
        public static string GetKeyWord()
        {
            string keyword = "";
            string kqurl = HttpContext.Current.Request.RawUrl.Replace("/gs_Admin", "");
            if (kqurl.ToString().Trim() != "/Index.aspx")
            {
                keyword = StringHelper.GetStrArray(StringHelper.GetStrArray(kqurl, '/')[1].Replace(".aspx", "").ToString(), '?')[0].ToString();
                if (!string.IsNullOrEmpty(HttpContext.Current.Request["rkeyword"]))
                {
                    keyword += "_" + HttpContext.Current.Request["rkeyword"];
                }
                keyword = keyword.Replace("Change", "");
            }
            else
            {
                keyword = "index";
            }
            return keyword;
        }

        public static Sys_Modules GetModul(string keyword)
        {
            Sys_Modules tm = DBCaChe.GetModelList().Find(delegate(Sys_Modules tmd) { return tmd.Keyword == keyword; });
            return tm;
        }

    }
}